const houseTypeModel = require('../model/houseType');
const houseTypeRecordModel = require('../model/houseTypeRecord');
const mongoose = require('mongoose');

async function addHouseType (req, res, next, msg = '添加房型成功') {
    try {
        const userId = req.user.userId;
        const {
            title,
            area,
            swiper,
            // baseInstallation, // 基础设施
            breakfirst,
            bed,
            houseNum,
            type,
            price,
            cashPledge,
            smokeLess,
            maxPerson,
            attention,
            base
        } = req.body;

        const houseType = await houseTypeModel.create({
            title,
            area,
            houseNum,
            author: mongoose.Types.ObjectId(userId),
            swiper,
            // baseInstallation: baseInstallation.map(item => mongoose.Types.ObjectId(item)), // 基础设施
            base,
            breakfirst,
            bed,
            type,
            price,
            cashPledge,
            smokeLess,
            maxPerson,
            attention
        });

        const houseTypeRecord = await houseTypeRecordModel.create({
            houseType: houseType._id,
            title,
            area,
            houseNum,
            author: mongoose.Types.ObjectId(userId),
            swiper,
            // baseInstallation: baseInstallation.map(item => mongoose.Types.ObjectId(item)), // 基础设施
            breakfirst,
            bed,
            type,
            price,
            cashPledge,
            smokeLess,
            maxPerson,
            attention,
            base
        });

        await houseType.set({
            record: houseTypeRecord._id
        });
        await houseType.save();

        res.json({
            code: 200,
            msg: '添加房型成功'
        })
    } catch(err) {
        next(err)
    }
}

async function getHouseType (req, res, next) {
    try {
        let {pn = 1,size = 10} = req.query;
        pn = Number(pn);
        size = Number(size);

        let data = await houseTypeModel
            .find({isDeleted: 0})
            .select('-isDeleted')
            .limit(size)
            .skip((pn-1)*size)


        res.json({
            code: 200,
            data
        })
    } catch(err) {
        next(err)
    }
}

async function getHouseTypeById (req, res, next) {
    try {
        const {id} = req.params;
        const data = await houseTypeModel.findById(id);
        res.json({
            code: 200,
            data
        })
    } catch(err) {
        next(err)
    }
}

async function updateHouseType (req, res, next) {
    try {
        const {userId} = req.user;
        const {
            title,
            houseType, // 房型id
            area,
            swiper,
            baseInstallation, // 基础设施
            breakfirst,
            bed,
            houseNum,
            type,
            price,
            cashPledge,
            smokeLess,
            maxPerson,
            attention
        } = req.body;

        const houseTypeData = await houseTypeModel.findById(houseType);
        if(houseTypeData){
            const houseTypeRecordData = await houseTypeRecordModel.create({
                title,
                author: userId,
                houseType: mongoose.Types.ObjectId(houseType), // 房型id
                area,
                swiper,
                houseNum,
                baseInstallation, // 基础设施
                breakfirst,
                bed,
                type,
                price,
                cashPledge,
                smokeLess,
                maxPerson,
                attention
            })

            await houseTypeData.set({
                record: houseTypeRecordData._id,
                title,
                author: userId,
                area,
                houseNum,
                swiper,
                baseInstallation, // 基础设施
                breakfirst,
                bed,
                type,
                price,
                cashPledge,
                smokeLess,
                maxPerson,
                attention
            })
            await houseTypeData.save();

            res.json({
                code: 200,
                msg: '修改房型信息成功'
            })
        } else {
            res.json({
                code: 200,
                msg: '该房型不存在'
            })
        }
    } catch(err) {
        next(err)
    }
}

async function removeHouseType (req, res, next) {
    try {
        const {houseType} = req.params;
        const {userId} = req.user;
        const houseTypeData = await houseTypeModel.findById(houseType);
        const houseTypeJson = houseTypeData.toJSON();
        delete houseTypeJson._id;
        if(houseTypeData){
            const record = await houseTypeRecordModel.create({
                ...houseTypeJson,
                houseType: houseTypeData._id,
                isDeleted: 1,
                author: userId
            });
            await houseTypeData.set({
                isDeleted: 1,
                author: userId,
                record: record._id
            });
            await houseTypeData.save();
            res.json({
                code: 200,
                msg: '删除房型成功'
            })
        } else {
            res.json({
                code: 400,
                msg: '找不到该房型'
            })
        }
    } catch(err) {
        next(err)
    }
}

async function getHouseTypeReduce (req, res, next) {
    try {
        
    } catch(err) {
        next(err)
    }
}

module.exports = {
    addHouseType,
    getHouseType,
    updateHouseType,
    removeHouseType,
    getHouseTypeById
}
